#import combine_csv
#import numpy
import arcpy
import os
import csv
import datetime
import calendar

# allowing for files to be overwritten
arcpy.env.overwriteOutput=True

spatialref = arcpy.SpatialReference(4326) # Create the spatial reference object as WGS84. Can modify if desired.



incsv_tag = r"%s\%s\%02d_%s_%s\AIS_%s_%02d_Zone%02d.csv"      # CSV in % (year, m, month, year, year, m, zone)
out_gdb_tag = r"%s\%s\%02d_%s_%s\merged.gdb"          # output gdb % (year,month,month_text,year)
out_name = "points_wc"
#outfc_tag = r"G:\data\2015\01_January_2015\merged.gdb\points_wc"                         


def main() :

##    print "Create Template"
##    ### only do this once
##    if arcpy.Exists(template):
##        arcpy.Delete_management(template) 
##    
##    arcpy.CreateFeatureclass_management(os.path.dirname(template), os.path.basename(template), "POINT", None, None, None, spatialref)
##    
##    arcpy.AddField_management(template, "MMSI", "Long")
##    arcpy.AddField_management(template, "BaseDateTime", "Date")
##    arcpy.AddField_management(template, "SOG", "Float")
##    arcpy.AddField_management(template, "COG", "Float")
##    arcpy.AddField_management(template, "Heading", "Long")
##    arcpy.AddField_management(template, "VesselName", "TEXT")
##    arcpy.AddField_management(template, "IMO", "TEXT")
##    ##arcpy.AddField_management(template, "CallSign", "TEXT")
##    arcpy.AddField_management(template, "VesselType", "Long")
##    ##arcpy.AddField_management(template, "Status", "TEXT")
##    arcpy.AddField_management(template, "Length", "Float")
##    arcpy.AddField_management(template, "Width", "Float")
##    arcpy.AddField_management(template, "Draft", "Float")
##    ##arcpy.AddField_management(template, "Cargo", "Long")


    print "Testing"
    zones = [8]
   
    join_AIS_CSV(zones,2015,1) 
        

def join_AIS_CSV(zones,year,month,folder) :
    '''
    Joins AIS CSV files into single point file that can be used with trackbuilder
    '''

    template = r"%s\templates.gdb\broadcast1516" % folder

    out_gdb = out_gdb_tag % (folder,year,month,calendar.month_name[month],year)
    outfc = os.path.join(out_gdb,out_name)
    
    data_names = ['MMSI', 'BaseDateTime', 'LAT', 'LON', 'SOG', 'COG', 'Heading', 'VesselName', 'IMO', 'CallSign', 'VesselType', 'Status', 'Length', 'Width', 'Draft', 'Cargo']
    data_names_small = ['SOG','COG','Heading','VesselName','IMO','VesselType','Length','Width','Draft']

    # index of other variables
    other_vars_ind = []
    for nm in data_names_small :
        other_vars_ind.append(data_names.index(nm))

    # Create output gdb if needed
    if not arcpy.Exists(out_gdb) == True :
        print "Create Output GDB"
        #print os.path.dirname(out_gdb) , os.path.basename(out_gdb)
        arcpy.CreateFileGDB_management( os.path.dirname(out_gdb) , os.path.basename(out_gdb) )

    # Create the output feature class if needed.
    print "Create Output FC"
    if arcpy.Exists(outfc):
        arcpy.Delete_management(outfc)

    # saving directly seems faster
    fc_mem = outfc # os.path.join("in_memory",os.path.basename(outfc))
    arcpy.CreateFeatureclass_management(os.path.dirname(fc_mem), os.path.basename(outfc), "POINT", template, None, None, spatialref)

    #print "write to FC"
    #                                                    MMSI, BaseDate +  All other cols
    cursor = arcpy.da.InsertCursor(fc_mem, ['SHAPE@XY'] +  data_names[0:2] + data_names_small ) # Create InsertCursor.

    #start = time.time()
    for zone in zones :

        incsv = incsv_tag % (folder, year, month, calendar.month_name[month], year, year, month, zone)
        
        print "Zone {0}".format(zone)
        #print "Open CSV reader"
        csv.register_dialect("xls", delimiter=",", lineterminator="\n") # Register the dialect for native CSV syntax in Microsoft Excel.
        f = open(incsv  , "r") 
        reader = csv.reader(f, dialect = "xls")

        # headers are MMSI, BaseDateTime, Lat, Lon, SOG ....
        headers = reader.next() # Read the first line as the header names.
        
        count = 0
        for row in reader:
            if count % 500000 == 0:
                print "processing row {0}".format(count)
                
            if float(row[4])>2.5 and float(row[0])>201000000 and float(row[0])<=775999999 : # skipping rows with low speed and invalid MMSI
                Ycoord = row[2] # Make sure 'Lat' is in the 3rd column.
                Xcoord = row[3] # Make sure 'Lon' is in the 4th column.

                # dealing with date
                dt = datetime.datetime.strptime(row[1], '%Y-%m-%dT%H:%M:%S')

                # get other data
                otherCols = [ row[i] for i in other_vars_ind ]
                
                #                                           MMSI + BaseDate +  All other cols
                newrow = [(float(Xcoord), float(Ycoord))] + [int(row[0])] + [dt] + otherCols
                newrow  = [None if v is '' else v for v in newrow] # replacing missing with None
                #print newrow
                cursor.insertRow(newrow) # Insert point in FC for each row in CSV.

            count += 1

        f.close()
        
    del cursor
            
    #print("--- %s seconds ---" % (time.time() - start))

if __name__ == '__main__':
    main()



